package mailmessages;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileOutputStream;

public class HtmlMessage {
    public static MimeMessage generate() throws Exception
    {
        String from = "428904315@qq.com "; // 发件人地址
        String to = "2623730553@qq.com"; // 收件人地址

        String subject = "中国黄色网冏冏";
        String body = "<a href=http://v.6.cn/event/promimg/bdPromotion.php?todbProm=true&src=z9weij1302/>"
                + "<h4>张先生,你好，欢淫成为我们1118位会员，详情点击查看</h4></a></br>"
                + "<img src=\"http://39.106.19.97/wp-content/uploads/2020/02/9f48497805.jpg\">";

        // 创建Session实例对象
        Session session = Session.getDefaultInstance(new Properties());
        // 创建MimeMessage实例对象
        MimeMessage message = new MimeMessage(session);
        // 设置发件人
        String nick="";
        try {
            nick=javax.mail.internet.MimeUtility.encodeText("妹妹色情服务");
        } catch (Exception e) {
            e.printStackTrace();
        }
        message.setFrom(new InternetAddress(nick+" <"+from+">"));
        // 设置收件人
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        // 设置发送日期
        message.setSentDate(new Date());
        // 设置邮件主题
        message.setSubject(subject);
        // 设置HTML格式的邮件正文
        message.setContent(body, "text/html;charset=gb2312");
        // 保存并生成最终的邮件内容
        message.saveChanges();

        // 把MimeMessage对象中的内容写入到文件中
        //msg.writeTo(new FileOutputStream("e:/HtmlMessage.eml"));
        return message;
    }
}
